package com.star.service;

import com.star.NotFoundException;
import com.star.pojo.dto.BlogListReq;
import com.star.repository.BlogRepository;
import com.star.pojo.Blog;
import com.star.pojo.vo.*;
import com.star.util.DateTimeUtil;
import com.star.util.MarkdownUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 博客服务
 *
 * @author tanyapeng
 * @date 2021/2/5
 */
@Service
public class BlogService {

    @Resource
    private BlogRepository blogRepository;

    /**
     * 通过id获取博客
     *
     * @param id
     * @return
     */
    public ShowBlog getBlogById(Long id) {
        return blogRepository.getBlogById(id);
    }

    /**
     * 获取所有博客
     *
     * @return
     */
    public List<BlogQuery> getAllBlog() {
        return blogRepository.getAllBlogQuery();
    }

    /**
     * 添加博客
     *
     * @param blog
     * @return
     */
    public int saveBlog(Blog blog) {
        blog.setCreateTime(LocalDateTime.now(DateTimeUtil.ZONE));
        blog.setUpdateTime(LocalDateTime.now(DateTimeUtil.ZONE));
        blog.setViews(0);
        blog.setCommentCount(0);
        return blogRepository.saveBlog(blog);
    }

    /**
     * 更新博客
     *
     * @param showBlog
     * @return
     */
    public int updateBlog(ShowBlog showBlog) {
        showBlog.setUpdateTime(new Date());
        return blogRepository.updateBlog(showBlog);
    }

    /**
     * 删除博客
     *
     * @param id
     */
    public void deleteBlog(Long id) {
        blogRepository.deleteBlog(id);
    }

    /**
     * 搜索博客
     *
     * @param searchBlog
     * @return
     */
    public List<BlogQuery> getBlogBySearch(SearchBlog searchBlog) {
        return blogRepository.searchByTitleOrTypeOrRecommend(searchBlog);
    }

    /**
     * 获取所有博客
     *
     * @return
     */
    public List<FirstPageBlog> getAllFirstPageBlog(BlogListReq req) {
        return blogRepository.selectFirstPageBlog(req);
    }

    /**
     * 获取所有博客数量
     *
     * @return
     */
    public int getAllFirstPageBlogCount() {
        return blogRepository.selectFirstPageBlogCount();
    }

    /**
     * 获取所有推荐博客
     *
     * @return
     */
    public List<RecommendBlog> getRecommendedBlog() {
        List<RecommendBlog> allRecommendBlog = blogRepository.selectAllRecommendBlog();
        return allRecommendBlog;
    }

//    public List<FirstPageBlog> getNewBlog() {
//        return blogDao.getNewBlog();
//    }

    /**
     * 搜索博客
     *
     * @param query
     * @return
     */
    public List<FirstPageBlog> getSearchBlog(String query) {
        return blogRepository.getSearchBlog(query);
    }

    /**
     * 获取博客信息
     *
     * @param id
     * @return
     */
    public DetailedBlog getDetailedBlog(Long id) {
        System.out.println(id);
        DetailedBlog detailedBlog = blogRepository.getDetailedBlog(id);
        if (detailedBlog == null) {
            throw new NotFoundException("该博客不存在");
        }
        String content = detailedBlog.getContent();
        detailedBlog.setContent(MarkdownUtils.markdownToHtmlExtensions(content));
        // 文章访问数量自增
        blogRepository.updateViews(id);
        // 文章评论数量更新
        blogRepository.getCommentCountById(id);
        return detailedBlog;
    }

    /**
     * 通过类型id获取友链
     *
     * @param typeId
     * @return
     */
    public List<FirstPageBlog> getByTypeId(Long typeId) {
        return blogRepository.getByTypeId(typeId);
    }

    /**
     * 获取博客总数
     *
     * @return
     */
    public Integer getBlogTotal() {
        return blogRepository.getBlogTotal();
    }

    /**
     * 获取博客观看数
     *
     * @return
     */
    public Integer getBlogViewTotal() {
        return blogRepository.getBlogViewTotal();
    }

    /**
     * 获取博客评论数
     *
     * @return
     */
    public Integer getBlogCommentTotal() {
        return blogRepository.getBlogCommentTotal();
    }

    /**
     * 获取博客消息数
     *
     * @return
     */
    public Integer getBlogMessageTotal() {
        return blogRepository.getBlogMessageTotal();
    }

}
